package com.sirap.spring.boot.demo.controller.album;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirap.basic.tool.C;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.Colls;
import com.sirap.basic.util.DateUtil;
import com.sirap.basic.util.MathUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.basic.util.XCodeUtil;
import com.sirap.fish.fetchers.EaseImageFetchers;
import com.sirap.fish.fetchers.PhoenixImageFetchers;
import com.sirap.fish.fetchers.SinaStoryImageFetchers;

@Controller
@RequestMapping(value = "/albumzai")
public class AlbumZaiController extends TopAlbumController {

	@Override
	@RequestMapping(value = "/list")
	public String listAlbums(HttpServletRequest request, HashMap<String, Object> map) {
		D.at();
		
		return StrUtil.occupy("album/album{0}List", type());
	}

	@Override
	@ResponseBody
    @RequestMapping(value = "/page/{page}")
	public Map<String, Object> getAlbumsByPage(@PathVariable int page, HttpServletRequest request) {
    	D.at();
    	List<Map<String, Object>> all = PhoenixImageFetchers.getAlbums();
		List<Map<String, Object>> photos = Colls.byPage(all, page, PAGE_SIZE_TEN);
		C.listSome(photos, 5);

		Map<String, Object> map = Amaps.newConcurrentHashMap();
		int totalpage = MathUtil.totalPageOf(all.size(), PAGE_SIZE_TEN);
		
		map.put("totalpage", totalpage);
		map.put("photos", photos);
	
		return map;
	}

	@Override
	@RequestMapping(value = "/show/{base64url}")
	public String showAlbum(HttpServletRequest request, @PathVariable String base64url, HashMap<String, Object> map) {
		D.at();
		map.putAll(getAlbumDetail(base64url, PhoenixImageFetchers.class));
		
		String whenstr = request.getParameter("when");
		if(whenstr != null) {
			try {
				Date when = DateUtil.dateOf(whenstr, DateUtil.DATE_TIME);
				map.put("timeago", DateUtil.timeAgo(when));
			} catch (Exception ex) {
				map.put("timeago", whenstr);
			}
		}
    	D.pjsp(map);
	
		return StrUtil.occupy("album/album{0}Detail", type());
	}

	@Override
	@ResponseBody
	@RequestMapping(value = "/count/{base64url}")
	public Map<String, Object> countOfImages(HttpServletRequest request, @PathVariable String base64url) {
		D.at();
		
    	String niceurl = XCodeUtil.fromBase64(base64url);
    	D.pla(base64url, niceurl);
    	
		return Amaps.createMap("count", PhoenixImageFetchers.countOfImages(niceurl));
	}

}
